<?php
include_once 'MySQLDAO.php';

/**
 * Definition of Class Bookings
 * This class represents a data access object for the database table 'bookings'.
 */
class Bookings {


	public $dao = null;

	private $i = 1;

	/**
	 * associated database field: bookingId, type: int, is nullable: false
	 */
	private $bookingId  = 0;

	/**
	 * associated database field: customerId, type: int, is nullable: false
	 */
	private $customerId;

	/**
	 * associated database field: dateBookingMade, type: date, is nullable: true
	 */
	private $dateBookingMade;

	/**
	 * associated database field: dateFrom, type: date, is nullable: true
	 */
	private $dateFrom;

	/**
	 * associated database field: dateTo, type: date, is nullable: true
	 */
	private $dateTo;

	/**
	 * associated database field: paymentDueDate, type: date, is nullable: true
	 */
	private $paymentDueDate;

	/**
	 * associated database field: paymentDueAmount, type: float, is nullable: true
	 */
	private $paymentDueAmount;

	/**
	 * associated database field: paymentMadeOn, type: date, is nullable: true
	 */
	private $paymentMadeOn;

	/**
	 * associated database field: comments, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $comments;

	private $roombookingsArray = null;

	private $paymentsArray = null;

	private $customers = null;



	/**
	 * Default constructor
	 * @param value some value
	 */
	function __construct(){
		$this->dao = new MySQLDao("Bookings");
	}


	/**
	 * Initialize the business object with data read from the DB.
	 * @param row database row array containing the values from one read record
	 */
	public function init($row){
		if(isset($row['bookingId'])) $this->bookingId = $row['bookingId'];
		if(isset($row['customerId'])) $this->customerId = $row['customerId'];
		if(isset($row['dateBookingMade'])) $this->dateBookingMade = $row['dateBookingMade'];
		if(isset($row['dateFrom'])) $this->dateFrom = $row['dateFrom'];
		if(isset($row['dateTo'])) $this->dateTo = $row['dateTo'];
		if(isset($row['paymentDueDate'])) $this->paymentDueDate = $row['paymentDueDate'];
		if(isset($row['paymentDueAmount'])) $this->paymentDueAmount = $row['paymentDueAmount'];
		if(isset($row['paymentMadeOn'])) $this->paymentMadeOn = $row['paymentMadeOn'];
		if(isset($row['comments'])) $this->comments = $row['comments'];
	}


	/**
	 * Returns the string representation for this object
	 * @param escapeQuotationMarks boolean value to indicate if quotation marks in value strings should be escaped, default value is true
	 * @return String String representation of Bookings
	 */
	public function toString($escapeQuotationMarks=true){
		$s = '[';
		$s .= 'bookingId: '.(($escapeQuotationMarks===true) ? addslashes($this->bookingId) : $this->bookingId).', ';
		$s .= 'customerId: '.(($escapeQuotationMarks===true) ? addslashes($this->customerId) : $this->customerId).', ';
		$s .= 'dateBookingMade: '.(($escapeQuotationMarks===true) ? addslashes($this->dateBookingMade) : $this->dateBookingMade).', ';
		$s .= 'dateFrom: '.(($escapeQuotationMarks===true) ? addslashes($this->dateFrom) : $this->dateFrom).', ';
		$s .= 'dateTo: '.(($escapeQuotationMarks===true) ? addslashes($this->dateTo) : $this->dateTo).', ';
		$s .= 'paymentDueDate: '.(($escapeQuotationMarks===true) ? addslashes($this->paymentDueDate) : $this->paymentDueDate).', ';
		$s .= 'paymentDueAmount: '.(($escapeQuotationMarks===true) ? addslashes($this->paymentDueAmount) : $this->paymentDueAmount).', ';
		$s .= 'paymentMadeOn: '.(($escapeQuotationMarks===true) ? addslashes($this->paymentMadeOn) : $this->paymentMadeOn).', ';
		$s .= 'comments: '.(($escapeQuotationMarks===true) ? addslashes($this->comments) : $this->comments);
		$s .= ']';
		return $s;
	}


	/**
	 * Returns the JSON (JavaScript Object Notation) string representation for this object
	 * @return String JSON representation of Bookings
	 */
	public function toJSONString(){
		$fieldValues = array ("bookingId"=>$this->bookingId, "customerId"=>$this->customerId, "dateBookingMade"=>$this->dateBookingMade, "dateFrom"=>$this->dateFrom, "dateTo"=>$this->dateTo, "paymentDueDate"=>$this->paymentDueDate, "paymentDueAmount"=>$this->paymentDueAmount, "paymentMadeOn"=>$this->paymentMadeOn, "comments"=>$this->comments);
		return json_encode($fieldValues);
	}


	/**
	 * Returns a HTML representation of this object using DIVs for formatting via css
	 * @param escapeHTML boolean value to indicate if value strings should be html escaped, default value is true
	 * @return String the HTML representation of the Bookings object
	 */
	public function toHTMLString($escapeHTML=true){
		$htmlCode = "<div class=\"bookings\">\n";
		$htmlCode .= '	<div class="bookings_bookingId">'.(($escapeHTML===true) ? htmlspecialchars($this->bookingId) : $this->bookingId)."</div>\n";
		$htmlCode .= '	<div class="bookings_customerId">'.(($escapeHTML===true) ? htmlspecialchars($this->customerId) : $this->customerId)."</div>\n";
		$htmlCode .= '	<div class="bookings_dateBookingMade">'.(($escapeHTML===true) ? htmlspecialchars($this->dateBookingMade) : $this->dateBookingMade)."</div>\n";
		$htmlCode .= '	<div class="bookings_dateFrom">'.(($escapeHTML===true) ? htmlspecialchars($this->dateFrom) : $this->dateFrom)."</div>\n";
		$htmlCode .= '	<div class="bookings_dateTo">'.(($escapeHTML===true) ? htmlspecialchars($this->dateTo) : $this->dateTo)."</div>\n";
		$htmlCode .= '	<div class="bookings_paymentDueDate">'.(($escapeHTML===true) ? htmlspecialchars($this->paymentDueDate) : $this->paymentDueDate)."</div>\n";
		$htmlCode .= '	<div class="bookings_paymentDueAmount">'.(($escapeHTML===true) ? htmlspecialchars($this->paymentDueAmount) : $this->paymentDueAmount)."</div>\n";
		$htmlCode .= '	<div class="bookings_paymentMadeOn">'.(($escapeHTML===true) ? htmlspecialchars($this->paymentMadeOn) : $this->paymentMadeOn)."</div>\n";
		$htmlCode .= '	<div class="bookings_comments">'.(($escapeHTML===true) ? htmlspecialchars($this->comments) : $this->comments)."</div>\n";
		$htmlCode .= "</div>\n";
		return $htmlCode;
	}
	/*
	public function getData($where) {
		$rows = $this->dao->getData($where);
		return $rows;
	}*/
	
	public static function deleteRecords($where) {
		$bookings = new Bookings();
		$bookings->dao->deleteRecord ($where);
		return;
	}

	public static function loadAll(){
		$bookings = new Bookings();
		$rows = $bookings->dao->getData("");
		$bookingss = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Bookings();
			$d->init($rows[$index]);
			array_push($bookingss,$d);
		}
		return $bookingss;
	}


	/**
	 * Load the Bookings uniquely by its primary key.
	 * @param bookingId the primary key
	 * @return Bookings Instance of {@link Bookings}
	 */
	public static function loadUnique($bookingId){
		$bookings = new Bookings();
		$rows = $bookings->dao->getData("`bookingId`='$bookingId'");
		$bookings->init($rows[0]);
		return $bookings;
	}


	/**
	 * Load all records of Bookings uniquely by its foreign keys:
	 * customerId
	 * @param customerId foreign key
	 * @return array of Instance of {@link Bookings}
	 */
	public static function loadByFKCustomerid($customerId){
		$bookings = new Bookings();
		$rows = $bookings->dao->getData("`customerId`='$customerId'");
		$bookingss = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Bookings();
			$d->init($rows[$index]);
			array_push($bookingss,$d);
		}
		return $bookingss;
	}


	/**
	 * Insert this object into the DB
	 * @return number new id (auto increment value) genereated
	 */
	public function insert(){
		//$list = array("bookingId"=>$this->bookingId, "customerId"=>$this->customerId, "dateBookingMade"=>$this->dateBookingMade, "dateFrom"=>$this->dateFrom, "dateTo"=>$this->dateTo, "paymentDueDate"=>$this->paymentDueDate, "paymentDueAmount"=>$this->paymentDueAmount, "paymentMadeOn"=>$this->paymentMadeOn, "comments"=>$this->comments);
		$formatDateBookingMade = date('Y-m-d', strtotime($this->dateBookingMade));
		$formatDateFrom = date('Y-m-d', strtotime($this->dateFrom));
		$formatDateTo = date('Y-m-d', strtotime($this->dateTo));
		$formatPaymentDueDate = date('Y-m-d', strtotime($this->paymentDueDate));
		$formatPaymentMadeOn = date('Y-m-d', strtotime($this->paymentMadeOn));
		$list = array("customerId"=>$this->customerId, "dateBookingMade"=>$formatDateBookingMade, "dateFrom"=>$formatDateFrom, "dateTo"=>$formatDateTo, "paymentDueDate"=>$formatPaymentDueDate, "paymentDueAmount"=>$this->paymentDueAmount, "paymentMadeOn"=>$formatPaymentMadeOn, "comments"=>$this->comments);
		return $this->dao->insertRecord($list);
	}


	/**
	 * Update this object into the DB
	 * @return number the number of updated records
	 */
	public function update(){
		//$list = array("bookingId"=>$this->bookingId, "customerId"=>$this->customerId, "dateBookingMade"=>$this->dateBookingMade, "dateFrom"=>$this->dateFrom, "dateTo"=>$this->dateTo, "paymentDueDate"=>$this->paymentDueDate, "paymentDueAmount"=>$this->paymentDueAmount, "paymentMadeOn"=>$this->paymentMadeOn, "comments"=>$this->comments);
		$formatDateBookingMade = date('Y-m-d', strtotime($this->dateBookingMade));
		$formatDateFrom = date('Y-m-d', strtotime($this->dateFrom));
		$formatDateTo = date('Y-m-d', strtotime($this->dateTo));
		$formatPaymentDueDate = date('Y-m-d', strtotime($this->paymentDueDate));
		$formatPaymentMadeOn = date('Y-m-d', strtotime($this->paymentMadeOn));
		
		$list = array("customerId"=>$this->customerId, "dateBookingMade"=>$formatDateBookingMade, "dateFrom"=>$formatDateFrom, "dateTo"=>$formatDateTo, "paymentDueDate"=>$formatPaymentDueDate, "paymentDueAmount"=>$this->paymentDueAmount, "paymentMadeOn"=>$formatPaymentMadeOn, "comments"=>$this->comments);
		$where = "`bookingId`='$bookingId'";
		return $this->dao->updateRecord($list,$where);
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initPOST(){
		if (isset($_POST['bookingId'])){
			$this->bookingId = $_POST['bookingId'];
		}
		if (isset($_POST['customerId'])){
			$this->customerId = $_POST['customerId'];
		}
		if (isset($_POST['dateBookingMade'])){
			$this->dateBookingMade = $_POST['dateBookingMade'];
		}
		if (isset($_POST['dateFrom'])){
			$this->dateFrom = $_POST['dateFrom'];
		}
		if (isset($_POST['dateTo'])){
			$this->dateTo = $_POST['dateTo'];
		}
		if (isset($_POST['paymentDueDate'])){
			$this->paymentDueDate = $_POST['paymentDueDate'];
		}
		if (isset($_POST['paymentDueAmount'])){
			$this->paymentDueAmount = $_POST['paymentDueAmount'];
		}
		if (isset($_POST['paymentMadeOn'])){
			$this->paymentMadeOn = $_POST['paymentMadeOn'];
		}
		if (isset($_POST['comments'])){
			$this->comments = $_POST['comments'];
		}
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initGET(){
		if (isset($_GET['bookingId'])){
			$this->bookingId = $_GET['bookingId'];
		}
		if (isset($_GET['customerId'])){
			$this->customerId = $_GET['customerId'];
		}
		if (isset($_GET['dateBookingMade'])){
			$this->dateBookingMade = $_GET['dateBookingMade'];
		}
		if (isset($_GET['dateFrom'])){
			$this->dateFrom = $_GET['dateFrom'];
		}
		if (isset($_GET['dateTo'])){
			$this->dateTo = $_GET['dateTo'];
		}
		if (isset($_GET['paymentDueDate'])){
			$this->paymentDueDate = $_GET['paymentDueDate'];
		}
		if (isset($_GET['paymentDueAmount'])){
			$this->paymentDueAmount = $_GET['paymentDueAmount'];
		}
		if (isset($_GET['paymentMadeOn'])){
			$this->paymentMadeOn = $_GET['paymentMadeOn'];
		}
		if (isset($_GET['comments'])){
			$this->comments = $_GET['comments'];
		}
	}


	/**
	 * Returns the value from the database field bookingId.
	 * In the database the field bookingId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getBookingId(){
		return $this->bookingId;
	}


	/**
	 * Set the value from the database field bookingId.
	 * In the database the field bookingId is of type int.
	 * The database field is NOT nullable.
	 * @param bookingId String with the value for the field to be set
	 */
	public function setBookingId($bookingId){
		$this->bookingId = $bookingId;
	}


	/**
	 * Returns the value from the database field customerId.
	 * In the database the field customerId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getCustomerId(){
		return $this->customerId;
	}


	/**
	 * Set the value from the database field customerId.
	 * In the database the field customerId is of type int.
	 * The database field is NOT nullable.
	 * @param customerId String with the value for the field to be set
	 */
	public function setCustomerId($customerId){
		$this->customerId = $customerId;
	}


	/**
	 * Returns the value from the database field dateBookingMade.
	 * In the database the field dateBookingMade is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getDateBookingMade(){
		return $this->dateBookingMade;
	}


	/**
	 * Set the value from the database field dateBookingMade.
	 * In the database the field dateBookingMade is of type date.
	 * The database field is  nullable.
	 * @param dateBookingMade String with the value for the field to be set
	 */
	public function setDateBookingMade($dateBookingMade){
		$this->dateBookingMade = $dateBookingMade;
	}


	/**
	 * Returns the value from the database field dateFrom.
	 * In the database the field dateFrom is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getDateFrom(){
		return $this->dateFrom;
	}


	/**
	 * Set the value from the database field dateFrom.
	 * In the database the field dateFrom is of type date.
	 * The database field is  nullable.
	 * @param dateFrom String with the value for the field to be set
	 */
	public function setDateFrom($dateFrom){
		$this->dateFrom = $dateFrom;
	}


	/**
	 * Returns the value from the database field dateTo.
	 * In the database the field dateTo is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getDateTo(){
		return $this->dateTo;
	}


	/**
	 * Set the value from the database field dateTo.
	 * In the database the field dateTo is of type date.
	 * The database field is  nullable.
	 * @param dateTo String with the value for the field to be set
	 */
	public function setDateTo($dateTo){
		$this->dateTo = $dateTo;
	}


	/**
	 * Returns the value from the database field paymentDueDate.
	 * In the database the field paymentDueDate is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getPaymentDueDate(){
		return $this->paymentDueDate;
	}


	/**
	 * Set the value from the database field paymentDueDate.
	 * In the database the field paymentDueDate is of type date.
	 * The database field is  nullable.
	 * @param paymentDueDate String with the value for the field to be set
	 */
	public function setPaymentDueDate($paymentDueDate){
		$this->paymentDueDate = $paymentDueDate;
	}


	/**
	 * Returns the value from the database field paymentDueAmount.
	 * In the database the field paymentDueAmount is of type float.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getPaymentDueAmount(){
		return $this->paymentDueAmount;
	}


	/**
	 * Set the value from the database field paymentDueAmount.
	 * In the database the field paymentDueAmount is of type float.
	 * The database field is  nullable.
	 * @param paymentDueAmount String with the value for the field to be set
	 */
	public function setPaymentDueAmount($paymentDueAmount){
		$this->paymentDueAmount = $paymentDueAmount;
	}


	/**
	 * Returns the value from the database field paymentMadeOn.
	 * In the database the field paymentMadeOn is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getPaymentMadeOn(){
		return $this->paymentMadeOn;
	}


	/**
	 * Set the value from the database field paymentMadeOn.
	 * In the database the field paymentMadeOn is of type date.
	 * The database field is  nullable.
	 * @param paymentMadeOn String with the value for the field to be set
	 */
	public function setPaymentMadeOn($paymentMadeOn){
		$this->paymentMadeOn = $paymentMadeOn;
	}


	/**
	 * Returns the value from the database field comments.
	 * In the database the field comments is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getComments(){
		return $this->comments;
	}


	/**
	 * Set the value from the database field comments.
	 * In the database the field comments is of type varchar.
	 * The database field is  nullable.
	 * @param comments String with the value for the field to be set
	 */
	public function setComments($comments){
		if(strlen($comments) > 45) throw new Exception("comments does not allow strings longer than 45 characters. The submitted string contains ".strlen($comments)." characters.");
		$this->comments = $comments;
	}


	/**
	 * Returns an object array containing all linked Roombookings objects.
	 * A One to Many(1:n) relation exists between the Bookings and the Roombookings.
	 * @return Roombookings[] Array of Roombookings instances
	 */
	public function getRoombookingss(){
	if($this->roombookingsArray == null){
			$this->roombookingsArray = array();
			$temproombookings = new Roombookings();
			$dataRowsTemp = $temproombookings->dao->getData("`bookingId` = '$this->bookingId'");
			if(count($dataRowsTemp)  > 0)
			foreach($dataRowsTemp as $dataRow){
				$tempRoombookings = new Roombookings();
				$tempRoombookings->init($dataRow);
				array_push($this->roombookingsArray , $tempRoombookings);
			}
		}
		return $this->roombookingsArray;
	}


	/**
	 * Returns an object array containing all linked Payments objects.
	 * A One to Many(1:n) relation exists between the Bookings and the Payments.
	 * @return Payments[] Array of Payments instances
	 */
	public function getPaymentss(){
	if($this->paymentsArray == null){
			$this->paymentsArray = array();
			$temppayments = new Payments();
			$dataRowsTemp = $temppayments->dao->getData("`bookingId` = '$this->bookingId'");
			if(count($dataRowsTemp)  > 0)
			foreach($dataRowsTemp as $dataRow){
				$tempPayments = new Payments();
				$tempPayments->init($dataRow);
				array_push($this->paymentsArray , $tempPayments);
			}
		}
		return $this->paymentsArray;
	}


	/**
	 * Set links to related Roombookings instances. Existing links will be overwritten
	 * @param roombookingsArray array of Roombookings instances to be linked to this object instance
	 */
	public function setRoombookingss($roombookingsArray){
		foreach($roombookingsArray as $roombookings)
			$roombookings->bookingId = $this->bookingId;
		$this->roombookingsArray = $roombookingsArray;
	}


	/**
	 * Add a link to object of type Roombookings
	 * @param roombookings instance of Roombookings to be linked to this object instance
	 */
	public function addRoombookings($roombookings){
		$roombookings->bookingId = $this->bookingId;
		if($this->roombookingsArray == null) $this->roombookingsArray = array();
		array_push($this->roombookingsArray, $roombookings);
	}


	/**
	 * Set links to related Payments instances. Existing links will be overwritten
	 * @param paymentsArray array of Payments instances to be linked to this object instance
	 */
	public function setPaymentss($paymentsArray){
		foreach($paymentsArray as $payments)
			$payments->bookingId = $this->bookingId;
		$this->paymentsArray = $paymentsArray;
	}


	/**
	 * Add a link to object of type Payments
	 * @param payments instance of Payments to be linked to this object instance
	 */
	public function addPayments($payments){
		$payments->bookingId = $this->bookingId;
		if($this->paymentsArray == null) $this->paymentsArray = array();
		array_push($this->paymentsArray, $payments);
	}


	/**
	 * Returns an instance of the linked Customers object.
	 * A One to Many (1:n) relation exists between the Bookings and the Customers table.
	 * @return Customers the linked instance of Customers
	 */
	public function getCustomers(){
		if($this->customers == null){
			$this->customers = Customers::loadUnique($this->customerId);
		}
		return $this->customers;
	}


	/**
	 * Set a link to a related Customers instance
	 * @param targetObjectInstanceName instance of Customers to be linked to this object instance
	 */
	public function setCustomers($customers){
		$customers->customerId = $this->customerId;
		$this->customers = $customers;
	}
}
?>